package 第二周.第四课;

/*
 * @author  YxinMiracle
 * @date  2022-04-26 10:03
 * @Gitee: https://gitee.com/yxinmiracle
 */

import java.util.Arrays;

public class 航班预定统计 {
    public int[] corpFlightBookings(int[][] bookings, int n) {
        // 使用差分算法进行解答
        // 创建一个差分数组
        // 创建n+2保证不会越位
        int[] delter = new int[n+2];
        for (int i = 0; i < bookings.length; i++) {
            int first = bookings[i][0];
            int last = bookings[i][1];
            int seats = bookings[i][2];
            // 差分算法
            delter[first] += seats;
            delter[last+1] -= seats;
        }
        int[] sum = new int[n+1];
        for (int i = 1; i <= n; i++) {
            sum[i] = sum[i-1] + delter[i];
        }
        int[] result = new int[n];
        for (int i = 0; i < n; i++) {
            result[i] = sum[i+1];
        }
        return result;
    }

    public static void main(String[] args) {
        int[][] bookings = new int[][]{{1,2,10},{2,3,20},{2,5,25}};
        int n = 5;
        new 航班预定统计().corpFlightBookings(bookings,n);
    }

}
